package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.vo.carInfo.CarDictVo;
import com.ruoyi.system.domain.vo.carInfo.CarSpellDictVo;
import com.ruoyi.system.domain.vo.eCatalog.ECatalogVo;
import com.ruoyi.system.service.IProductCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CarInfoMapper;
import com.ruoyi.system.domain.CarInfo;
import com.ruoyi.system.service.ICarInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 汽车信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-25
 */
@Service
public class CarInfoServiceImpl extends ServiceImpl<CarInfoMapper, CarInfo> implements ICarInfoService
{
    @Autowired
    private CarInfoMapper carInfoMapper;

    @Autowired
    private IProductCarService productCarService;

    /**
     * 查询汽车信息
     *
     * @param id 汽车信息主键
     * @return 汽车信息
     */
    @Override
    public CarInfo selectCarInfoById(Long id)
    {
        return carInfoMapper.selectCarInfoById(id);
    }

    /**
     * 查询汽车信息列表
     *
     * @param carInfo 汽车信息
     * @return 汽车信息
     */
    @Override
    public List<CarInfo> selectCarInfoList(CarInfo carInfo)
    {
        return carInfoMapper.selectCarInfoList(carInfo);
    }

    /**
     * 查询电子目录dict
     *
     * @param eCatalogVo
     * @return 汽车信息
     */
    @Override
    public List<CarInfo> selectECatalogCarList(ECatalogVo eCatalogVo)
    {
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(StringUtils.isNotEmpty(eCatalogVo.getBrand()),"car_info.brand",eCatalogVo.getBrand());
//                .eq(StringUtils.isNotEmpty(eCatalogVo.getVin()),"car_info.vin",eCatalogVo.getVin());
        return carInfoMapper.selectList(queryWrapper);
    }

    /**
     * 查询电子目录车型dict
     *
     * @param eCatalogVo
     * @return String
     */
    @Override
    public List<String> selectCarTypeDict(ECatalogVo eCatalogVo)
    {
        List<CarInfo> carInfos = this.selectECatalogCarList(eCatalogVo);
        List<String> collect = carInfos.stream()
                .map(info -> info.getType())
                .distinct()
                .collect(Collectors.toList());
        return collect;
    }

    /**
     * 查询电子目录排量dict
     *
     * @param eCatalogVo
     * @return String
     */
    @Override
    public List<String> selectCarDisplacementDict(ECatalogVo eCatalogVo)
    {
        List<CarInfo> carInfos = this.selectECatalogCarList(eCatalogVo);
        List<String> collect = carInfos.stream()
                .map(info -> info.getDisplacement())
                .distinct()
                .collect(Collectors.toList());
        return collect;
    }

    /**
     * 查询汽车dict
     *
     * @param carInfo 汽车信息
     * @return 汽车信息vo
     */
    @Override
    public List<CarDictVo> selectCarDict(CarInfo carInfo) {
        return carInfoMapper.selectCarDict(carInfo);
    }

    /**
     * 查询汽车热门dict
     *
     * @param carInfo 汽车信息
     * @return 汽车信息
     */
    @Override
    public List<CarSpellDictVo> getHotDict(CarInfo carInfo) {
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(CarInfo::getBrand,CarInfo::getSpell)
                .orderByAsc(CarInfo::getHot);
        List<CarInfo> carInfos = carInfoMapper.selectList(queryWrapper);
        return CollectionUtils.convertCollectionType(carInfos,CarSpellDictVo.class);
    }

    /**
     * 查询汽车信息，vin
     *
     * @param eCatalogVo
     * @return 汽车信息
     */
    @Override
    public CarInfo getECatalogCar(ECatalogVo eCatalogVo) {
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CarInfo::getVin,eCatalogVo.getVin());
        return carInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public List<CarInfo> getApplicableModel(Long productId) {
        List<Long> modelId = productCarService.getApplicableModelId(productId);
        return carInfoMapper.selectBatchIds(modelId);
    }

    /**
     * 新增汽车信息
     *
     * @param carInfo 汽车信息
     * @return 结果
     */
    @Override
    public int insertCarInfo(CarInfo carInfo)
    {
        carInfo.setCreateTime(DateUtils.getNowDate());
        String pinyin = PinyinUtil.getFirstLetter(carInfo.getBrand(), "").toLowerCase();
        carInfo.setSpell(pinyin);
        return carInfoMapper.insertCarInfo(carInfo);
    }

    /**
     * 修改汽车信息
     *
     * @param carInfo 汽车信息
     * @return 结果
     */
    @Override
    public int updateCarInfo(CarInfo carInfo)
    {
        carInfo.setUpdateTime(DateUtils.getNowDate());
        String pinyin = PinyinUtil.getFirstLetter(carInfo.getBrand(), "").toLowerCase();
        carInfo.setSpell(pinyin);
        return carInfoMapper.updateCarInfo(carInfo);
    }

    /**
     * 批量删除汽车信息
     *
     * @param ids 需要删除的汽车信息主键
     * @return 结果
     */
    @Override
    public int deleteCarInfoByIds(Long[] ids)
    {
        return carInfoMapper.deleteCarInfoByIds(ids);
    }


    /**
     * 批量删除汽车信息以及对应中间表数据信息
     *
     * @param ids 需要删除的汽车信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCarByIds(Long[] ids){
        int delete = carInfoMapper.deleteCarInfoByIds(ids);
        if(delete <= 0){
            new RuntimeException("删除失败!");
        }
        Boolean carIds = productCarService.deleteByCarIds(ids);
        if (!carIds) {
            new RuntimeException("删除失败!");
        }
        return delete;
    }

    /**
     * 删除汽车信息信息
     *
     * @param id 汽车信息主键
     * @return 结果
     */
    @Override
    public int deleteCarInfoById(Long id)
    {
        return carInfoMapper.deleteCarInfoById(id);
    }

}
